<?php

namespace app\model;

use think\facade\Db;
use think\Model;

class BooksModel extends Model {

    protected $table ="books";

    protected $pk = 'book_id';

    private $cacheKey="book:id:";

    private $cacheTime=5184000;


    /*
       * 获取total
       * */
    public function getTotal($where=[]){
        return Db::name($this->table)->where($where)->count();
    }


    /*
     * 获取list
     * */
    public function getList($where=[],$page = null,$limit = null,$orderField = null,$orderType = "ASC"){

        $obj =  Db::name($this->table)->field($this->pk)->where($where);

        if(!empty($page) && !empty($limit)){
            $obj = $obj->page($page, $limit);
        }

        if(!empty($orderField) && !empty($orderType)){
            $obj = $obj->order($orderField, $orderType);
        }

        $list = $obj->select();

        if($list){
            $return = [];
            foreach($list as $item){
                $return[] = $this->getInfo($item[$this->pk]);
            }
            return $return;
        }
        return [];
    }


    /*
    * 获取推荐list
    * */
    public function getGuessList($where=[],$page = null,$limit = null,$orderField = null,$orderType = "ASC"){

        $obj =  Db::name($this->table)->field($this->pk)->where($where)->orderRaw("RAND()");

        if(!empty($page) && !empty($limit)){
            $obj = $obj->page($page, $limit);
        }

        if(!empty($orderField) && !empty($orderType)){
            $obj = $obj->order($orderField, $orderType);
        }

        $list = $obj->select();

        if($list){
            $return = [];
            foreach($list as $item){
                $return[] = $this->getInfo($item[$this->pk]);
            }
            return $return;
        }
        return [];
    }



    /*
   * 获取订单状态详情
   * */
    public function getInfo($id){
        $cacheKey = $this->cacheKey.$id;
        $redisModel = new RedisModel();
        $info = $redisModel->get($cacheKey);
        if($info){
            return json_decode($info,true);
        }

        $info = Db::name($this->table)->find($id);

        if($info){
            $redisModel->set($cacheKey,json_encode($info,256),$this->cacheTime);
            return $info;
        }
        return $info;
    }


    /*
* 更新条件更新订单
* */
    public function addOne($data){
        return Db::name($this->table)->insertAll($data);
    }



    /*
     * 获取用户的阅读进度
     * */
    public function getChapterFb($book_id){
        $bookInfo = $this->getInfo($book_id);
        $total = Db::name("book_chapters")->where("book_id",$book_id)->count();
        return (int) ($bookInfo["points"] / $total);
    }




    /*
     * 获取用户对某本书的阅读进度
     * */
    public function getUserBookProcess($uid,$book_id){
        $total = Db::name("book_chapters")->where("book_id",$book_id)->count();
        if($total == 0) return 0;
        $have =  Db::name("user_reading_records")->where(["book_id"=>$book_id,"uid"=>$uid])->count();
        $result = number_format($have / $total, 2, '.', '');
        $result *= 100;
        return $result > 100 ? 100 : $result;
    }


    /*
 * 更新条件更新订单
 * */
    public function updateAll($where,$data){
        return Db::name($this->table)->where($where)->update($data);
    }



    /*
     * 字段+1
     * */
    public function incrBookField($book_ids,$field,$num = 1){
        if(empty($book_ids) ){
            return false;
        }

        $where =[];
        $where[] =["book_id","IN",$book_ids] ;
        return Db::name($this->table)->where($where)->inc($field,$num)->update();
    }

    /*
       * 字段-1
       * */
    public function decBookField($book_ids,$field,$num = 1){
        if(empty($book_ids) ){
            return false;
        }

        $where =[];
        $where[] =["book_id","IN",$book_ids] ;
        return Db::name($this->table)->where($where)->dec($field,$num)->update();
    }
}